<?php
namespace common\components;

use yii;
use common\models\Catelog;
use common\models\Note;
use yii\helpers\Url;

class CatelogHelper {

    public static function categories($model_id = null) {
        if ($model_id !== null) {
            $query = Catelog::find()->where(['model_id' => $model_id])->asArray()->all();
        } else {
            $query = Catelog::find()->asArray()->all();
        }

        //  把category的idhepage_content的title组成 key->value 对
        foreach ($query as $key => $value) {
            if (isset($value)) {
                $cats[$key]['id'] = $value['id'];
                $cats[$key]['title'] = $value['title'];
            } else {
                return [];
            }
        }
        if ($cats !== null) {
            return $cats;
        } else {
            throw new \yii\web\NotFoundHttpException('The requested row does not exist.');
        }
    }

    public static function category_widget($cid) {
        if (isset($cid)) {
            $query = Catelog::find()->where(['cid' => $cid])->asArray()->all();
        } else {
            $query = [];
        }
        return $query;
    }
    
    public static function note_widget($limit, $cat_id) {
        if ($cat_id !== null && $limit !== null) {
            $query = Note::find()->where(['cat_id' => $cat_id])->andWhere(['enable'=>1])->limit($limit)->orderBy(['created_at' => SORT_DESC])->asArray()->all();
        } else {
            $query = Note::find()->where(['enable'=>1])->limit($limit)->orderBy(['created_at' => SORT_DESC])->asArray()->all();
        }
        return $query;
    }

    //  话题topic标签widget
    public static function tags_widget($cid = null) {
        if ($cid !== null) {
            $pidQuery = (new \yii\db\Query())->select('pid')->from('notes');
            $note_tags = Note::find()->select(['id', 'count' => $pidQuery])->where(['enable'=>1])->from('note');
        }
        $tags_merge = array_merge($note_tags);  //合并

        $tags = array();
        foreach ($tags_merge as $value) {
            if (strpos($value['tag'], ';') !== false) {
                $multi_tag = explode(';', $value['tag']);
                $tags = array_merge($multi_tag, $tags);   //合并多词tag
            } else {
                $tags[] = trim($value['tag']);
            }
        }
        $tags_arr = array_unique($tags);    //  数组去重
        return $tags_arr;
    }

    //  用于backend后台新建模块记录时，选择哪个栏目
    public static function CatelogSelect() {
        $catelog = Catelog::find()->select(['id', 'title'])->asArray()->all();

        foreach ($catelog as $key => $value) {
            if (isset($value)) {
                $cats[$key]['id'] = $value['id'];
                $cats[$key]['title'] = $value['title'];
            } else {
                return [];
            }
        }
        if ($cats !== null) {
            return $cats;
        } else {
            throw new \yii\web\NotFoundHttpException('The requested row does not exist.');
        }
    }
    
    // 更新浏览量 帮助函数
    public static function updateViews($model, $views, $id) {
        $views += 1;
        $connection = \Yii::$app->db;
        $connection->createCommand("UPDATE {$model} SET view={$views} WHERE id={$id}")->execute();
    }
    
    //  返回数组，用于layouts布局的menu动态显示各Career
    public static function CatelogMenu() {
        $query = Catelog::find()->select(['title', 'slug'])->where(['parent_id' => 0])->asArray()->all();

        //  把catelog的id和note的title组成 key->value 对
        $cats = [];
        foreach ($query as $key => $value) {
            if (isset($value)) {
                $cats[$key]['label'] = $value['title'];
                $cats[$key]['url'] = Url::to('/catelog/' . $value['slug'], true);
            } else {
                return [];
            }
        }
        if ($cats !== null) {
            return $cats;
        } else {
            throw new \yii\web\NotFoundHttpException('The requested row does not exist.');
        }
    }

    //  用于frontend前台，根据slug选择相应的内容
    public static function CatelogSlug($slug) {
        if ($slug !== null) {
            $catelog = Catelog::find()->select(['id', 'title', 'description', 'slug'])->where(['slug' => $slug])->asArray()->one();
            return $catelog;
        } else {
            return 0;
        }
    }    

    //  查詢用於widget的tag标签云数据
    public static function NoteTags($tag='') {
        if (empty($tag)) {
            $tags = Note::find()->select(['tag'])->where(['is not','tag',NULL])->andWhere(['enable'=>1])->groupBy(['tag'])->orderBy('view desc')->asArray()->all();
        }else{
            //  在标签页使用，不含本标签
            $tags = Note::find()->select(['tag'])->where(['!=','tag',$tag])->andWhere(['enable'=>1])->andWhere(['is not','tag',NULL])->groupBy(['tag'])->orderBy('view desc')->asArray()->all();
        }
        return $tags;
    }
}
